class HomeController < ApplicationController
  #popups :show_burndown
  helper :sparklines
  
  def index
    @sprint = session[:sprint]
    
    if !@sprint.nil?
      SortHelper.columns = %w(ticket_id name initial_estimate)
      SortHelper.default_order = %w(id name)
      @tasks = @sprint.tasks.sort do |a,b|
        SortHelper.sort(a,b,params)
      end    
      if params['show_inprogress'] 
        @tasks = @tasks.select { |task|
          task.current_estimate > 0
        }
      end
    end
  end
  
  def update_estimates
    @sprint = session[:sprint]
    date = Date.today
    @sprint.tasks.each do |t| 
      initial_estimate_param = "tasks_#{t.id}_initial_estimate"
      current_estimate_param = "tasks_#{t.id}_current_estimate"
      if params[initial_estimate_param]
        t.new_initial_estimate = params[initial_estimate_param].to_i
        t.save
      end
      if params[current_estimate_param]
        newEstimate = params[current_estimate_param].to_i
        t.update_estimate(date, newEstimate)
      end
    end
    
    redirect_to :action => 'index'
  end
  
  def show_burndown
    @sprint = Sprint.find(params[:id])
    render :layout => false
  end
  
  def calendar_details
    @sprint = session[:sprint]
  end
  
  def add_day
    @sprint = Sprint.find(params[:id])
    @sprint.number_of_days = @sprint.number_of_days + 1
    @sprint.save
    
    redirect_to :action => 'calendar_details'
    
  end
  
  def drop_day
    breakpoint
    @sprint = Sprint.find(params[:id])
    date = params[:date].to_date
    p "date: #{date}"
    @sprint.tasks.each do |task|
      task.estimates.each do |estimate|
        p "est: #{estimate.id}, #{estimate.date}"
        if (estimate.date == date)
          p "about to delete estimate #{estimate.id}"
          estimate.destroy
          #task.estimates.remove estimate
        end
      end
    end
    
    redirect_to :action => 'calendar_details'
  end
  
end